dojo._xdResourceLoaded(function(_1,_2,_3){
		return {
		depends:[
			["provide","ESI.dijit.AddDataButton"],
			["require","dijit._Widget"],
			["require","dijit.form.ComboBox"],
			["require","dijit.layout.ContentPane"],
			["require","dijit.layout.BorderContainer"],
			["require","dijit.Toolbar"],
			["require","dojox.grid.Grid"],
			["require","dojox.data.jsonPathStore"],
			["require","dojox.jsonPath.query"],
			["require","dojo.data.ItemFileReadStore"],
			["require","dojo.io.script"],
			["require","dijit.TooltipDialog"]
		],
		defineResource: function(dojo){
			dojo.provide("ESI.dijit.AddDataButton");
		
			dojo.require("dijit._Widget");
			dojo.require("dijit.layout.ContentPane");
			dojo.require("dijit.layout.BorderContainer");
			dojo.require("dijit.form.ComboBox");
			dojo.require("dijit.form.Button");
			dojo.require("dojox.grid.Grid");
			dojo.require("dojox.data.jsonPathStore");
			dojo.require("dojox.jsonPath.query");
			dojo.require("dojo.data.ItemFileReadStore");
			dojo.require("dojo.io.script");
			dojo.require("dijit.Toolbar");
			dojo.require("dijit.TooltipDialog");
			dojo.declare(
			'ESI.dijit.AddDataButton',
			[dijit._Widget, dijit._Templated],
			{
			    templateString: "<div dojoAttachPoint='containerNode' class='dijit dijitReset dijitLeft dijitInline'> <div dojoType='dijit.form.DropDownButton' dojoAttachPoint='dialog'><span>Add Data</span><div dojoType='dijit.TooltipDialog' title='Add Data' ><div dojoType='dijit.layout.BorderContainer' design='headline' style='height:250px;width:400px;'><div dojoType='dijit.layout.ContentPane' region='top' style='height:50px'><span> Layers In:</span><select dojoType='dijit.form.ComboBox' name='server' dojoAttachPoint='myComboBox' jsId='esi_adddata_servers' style='width:300px'></select><div dojoType='dijit.Toolbar' dojoAttachPoint='toolbar'><div dojoType='dijit.form.Button' label='Go Up' iconClass='goUpIcon' dojoAttachPoint='goUpButton' disabled='true'>	</div></div></div><div dojoType='dijit.layout.ContentPane' region='center'><table id='grid' dojoType='dojox.grid.Grid' jsId='grid' dojoAttachPoint='grid' style='height:150px'><thead><tr><th width='20' style='width:20p' field='icon' formatter='formatImage'>&nbsp;</th><th field='name'>Name</th><th field='type'>Type</th></tr></thead></table></div><div dojoType='dijit.layout.ContentPane' dojoAttachPoint='infoArea' region='bottom' style='width:100%;height:25px'/></div></div></div></div>",
			    templateCSSPath: dojo.moduleUrl("ESI.dijit", "css/AddDataButton.css"),
			    widgetsInTemplate: true,
			    servers: [],
			    images: {
				goUp: "arrowup.png"
			    },
			    postCreate: function() {
				this.inherited("postCreate", arguments);
				this.setUpComboBox();
				this.refreshGridForFolder();
			    },
			    setUpComboBox: function() {
				var that = this;
				var data = { identifier: "name", items: [] };
				var cnt = 0;
				dojo.forEach(this.servers, function(serv) {
				    data.items.push({ name: serv });
				    if (cnt === 0)
					that.myComboBox.setValue(serv);
				    cnt++;
			
				});
				var st = new dojo.data.ItemFileReadStore({ data: data });
				this.myComboBox.store = st;
				dojo.connect(this.myComboBox, "onChange", dojo.hitch(this, this.refreshGridForFolder));
				dojo.connect(this.grid, "onRowDblClick", dojo.hitch(this, this.selectService));
				dojo.connect(this.goUpButton, "onClick", dojo.hitch(this, this.goUp));
			    },
			    closeDialog: function() {
				this.dialog.reset();
			    },
			    selectService: function(evt) {
				var _row = this.grid.model.getRow(evt.rowIndex);
				var _server = this.myComboBox.getValue();
			
				if (_row.type === "Folder") {
				    this.myComboBox.setValue(_server + "/" + _row.name);
				    this.goUpButton.setDisabled(false);
		
				} else {
				    console.log("publishing " + _server + "/" + _row.name);
				    dojo.publish("/esi/add/mapservice", [_server, _row.name]);
				    this.closeDialog();
				}
			    },
			    refreshGridForFolder: function() {
				var _server = this.myComboBox.getValue();
				var _folder = "";
				console.log(_server);
				console.log(_folder);
				dojo.io.script.get({
				    url: _server + ((_folder === undefined) ? "" : ("/" + _folder)),
				    content: {
					f: "json"
				    },
				    load: function(data) {
					var newData = [];
					dojo.forEach(data.folders, function(folder) {
					    var _item = {};
					    _item.name = folder;
					    _item.type = "Folder";
					    _item.icon = "folder"
					    newData.push(_item);
					});
					dojo.forEach(data.services, function(service) {
					    if (service.type !== "MapServer")
						return;
					    var _item = {};
			
					    _item.name = service.name.substring(service.name.lastIndexOf("/") + 1);
					    _item.type = service.type;
					    _item.icon = '<div style="width:20px;height:20px" class="mapserviceicon"></div>';
					    newData.push(_item);
					});
			
					dStore = new dojox.data.jsonPathStore({ data: newData });
					dModel = new dojox.grid.data.DojoData(null, dStore, {});
					grid.setModel(dModel);
					grid.refresh();
			
				    },
				    callbackParamName: 'callback',
				    timeout: 10000,
				    error: dojo.hitch(this, function(resp) {
					this.infoArea.setContent("Error!  Services not found");
				    })
				});
			
			    },
			    isValueBase: function(val) {
				var _isBaseValue = false;
				dojo.forEach(this.servers, function(serv) {
				    if (serv === val)
					_isBaseValue = true;
				});
				return _isBaseValue;
			    },
			    goUp: function() {
			
				var _server = this.myComboBox.getValue();
				
				if (this.isValueBase(_server))
				    return;
			
				var _newVal = _server.substring(0, _server.lastIndexOf("/"));
				if (this.isValueBase(_newVal))
				    this.goUpButton.setDisabled(true);
				else    
				    this.goUpButton.setDisabled(false);
				this.myComboBox.setValue(_newVal);
			
			
			
			    },
			    formatImage:function(type){
			    	    alert("type");
			    	    if (type=="folder")
			    	    	    return "<img src='http://ruprict-gadgets.googlecode.com/svn/trunk/ESI/dijit/css/folder.png'/>";
			    	    else
			    	    	    return "<img src='http://ruprict-gadgets.googlecode.com/svn/trunk/ESI/dijit/css/map.png'/>";
			    }
			});
		}}
});

